import { defineStore } from 'pinia'
import { useI18n } from 'vue-i18n'

export const useDialogStore = () => {
  const { t } = useI18n()

  return defineStore({
    id: 'dialog',
    state: () => ({
      active: false,
      title: '',
      message: '',
      size: 'md',  // sm || md (default) || lg
      data: null,
      variant: 'danger', // primary || danger
      yesLabel: t('general.yes'),
      noLabel: t('general.no'),
      resolve: null,
      hideNoButton: false,
    }),
    actions: {
      openDialog(data) {
        this.active = true
        this.title = data.title
        this.message = data.message
        this.size = data.size
        this.data = data.data
        this.variant = data.variant
        this.yesLabel = data.yesLabel
        this.noLabel = data.noLabel
        this.hideNoButton = data.hideNoButton

        return new Promise((resolve, reject) => {
          this.resolve = resolve
        })
      },
      closeDialog() {
        this.active = false

        setTimeout(() => {
          this.title = ''
          this.message = ''
          this.data = null
        }, 300)
      },
    },
  })()
}
